package com.transparent;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtility {
	  public static BufferedImage makeColorTransparent(String pathToSource, int red, int green, int blue) throws IOException {
		    // read original image with no transparency
		    BufferedImage original = ImageIO.read(new File(pathToSource));
		 
		    // get dimensions of image
		    int width = original.getWidth();
		    int height = original.getHeight();
		 
		    // create a new image that will have transparent colors
		    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
		 
		    // make color transparent
		    int oldRGB = new Color(red, green, blue).getRGB();
		    int newRGB = new Color(red, green, blue, 0).getRGB();
		    int currRGB;
		 
		    for (int x = 0; x < width; x++) {
		      for (int y = 0; y < height; y++) {
		        currRGB = original.getRGB(x, y);
		 
		        if (oldRGB == currRGB) {
		          result.setRGB(x, y, newRGB);
		        } else {
		          result.setRGB(x, y, currRGB);
		        }
		      }
		    }
		    return result;
		  }
}
